<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入内容" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格数据 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="编号" width="140"></el-table-column>
        <el-table-column label="商品名称" prop="goodsName"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goodsPrice" width="100px"></el-table-column>
        <el-table-column label="商品重量(克)" prop="goodsWeight" width="70px"></el-table-column>
        <el-table-column label="商品数量" prop="goodsNumber" width="70px"></el-table-column>
        <el-table-column label="创建时间" prop="addTime" width="140px">
            <template slot-scope="scope">{{scope.row.addTime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button style="margin-right: 10px" type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog"></el-button>
            <el-popconfirm @confirm="removeById(scope.$index, scope.row)"
                           title="确定删除该商品吗?">
            <el-button
              type="danger"
              icon="el-icon-delete"
              slot="reference"
              size="mini"
            ></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
        title="修改商品"
        :visible.sync="addressDialogVisible"
        width="50%"
        @close="addressDialogClosed"
    >
      <el-form
          :model="addressForm"
          label-width="100px"
      >
        <el-form-item label="商品名称" prop="address2">
          <el-input v-model="goodsList.goodsName"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="address2">
          <el-input v-model="goodsList.goodsPrice"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="address2">
          <el-input v-model="goodsList.goodsNumber"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="address2">
          <el-input v-model="goodsList.goodsWeight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 商品列表
      goodsList: [],
      // 修改地址对话框
      addressDialogVisible: false,
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    showEditDialog () {
      this.addressDialogVisible = true
    },
    getGoodsList () {
        //发请求获取所有商品信息
        let url = "http://localhost:8080/goods/list"
        this.axios.get(url).then((response) => {
          this.goodsList = response.data.data;
        })
    },
   /* handleSizeChange (newSize) {
      this.getGoodsList()
    },
    handleCurrentChange (newSize) {
      this.getGoodsList()
    },*/

   /* // 删除
    bannerDelete(index, bannerArr) {
      let url = "http://localhost:8080/banner/delete?id=" + bannerArr.id
      this.axios.get(url).then((response) => {
        this.getBannerList()
      })
    },*/

    // 通过Id删除商品
    removeById (index,goodsList) {
      let url = "http://localhost:8080/goods/delete?id=" + goodsList.id
      this.axios.get(url).then((response) => {
        this.getGoodsList()
      })
    },
    goAddPage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
